package com.jichangxiu.server.platform.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.server.platform.entity.dto.PlatformUserQueryDto;
import com.jichangxiu.server.platform.entity.vo.PlatformUserAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformUserEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformUserQueryVo;
import com.jichangxiu.server.platform.service.PlatformUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * PlatformUserAction:
 * 平台-用户管理控制器
 *
 * @author Jcx
 * @create 2023-02-25 22:10
 */
@Slf4j
@Rest("/platform/user")
@Tag(name = "平台-用户管理控制器")
public class PlatformUserAction extends BaseAction {

    @Resource
    private PlatformUserService platformUserService;

    /**
     * 新增用户
     *
     * @param platformUserAddVo 用户视图对象
     * @return 是否成功
     */
    @Post("/add")
    @Operation(summary = "平台-新增用户")
    @PreAuthorize("@ss.hasPermission('platform:user:add')")
    public String add(@RequestBody PlatformUserAddVo platformUserAddVo) {
        return toStr(platformUserService.add(platformUserAddVo));
    }

    /**
     * 多选级联删除用户
     *
     * @param userIds 用户ID列表
     * @return 是否成功
     */
    @Delete("/del")
    @Operation(summary = "平台-多选级联删除用户")
    @PreAuthorize("@ss.hasPermission('platform:user:del')")
    public String del(@RequestBody String[] userIds) {
        return toStr(platformUserService.del(ListUtil.toList(userIds)));
    }

    /**
     * 修改用户信息
     *
     * @param platformUserEditVo 用户视图对象
     * @return 是否成功
     */
    @Put("/edit")
    @Operation(summary = "平台-修改用户信息")
    @PreAuthorize("@ss.hasPermission('platform:user:edit')")
    public String edit(@RequestBody PlatformUserEditVo platformUserEditVo) {
        return toStr(platformUserService.edit(platformUserEditVo));
    }

    /**
     * 根据ID获取用户详情
     *
     * @param userId 用户ID
     * @return 用户详情
     */
    @Get("/info/{userId}")
    @Operation(summary = "平台-根据ID获取用户详情")
    @PreAuthorize("@ss.hasPermission('platform:user:info')")
    public PlatformUserQueryDto info(@PathVariable("userId") @NotBlank(message = "用户ID不能为空") String userId) {
        return platformUserService.info(userId);
    }

    /**
     * 分页查询用户列表
     *
     * @param platformUserQueryVo 用户视图对象
     * @return 分页用户列表
     */
    @Get(value = "/pageList")
    @Operation(summary = "平台-分页查询用户列表")
    @PreAuthorize("@ss.hasPermission('platform:user:pageList')")
    public PageData<PlatformUserQueryDto> pageList(@ParameterObject PlatformUserQueryVo platformUserQueryVo) {
        return platformUserService.pageList(platformUserQueryVo);
    }

}
